# Load necessary libraries
library(readxl)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmlwidgets)
# Load the Excel sheets
imports_data <- read_excel("/Users/kabeer/Downloads/import_ch_NK.xlsx", sheet = "Sheet1")
gdp_data <- read_excel("/Users/kabeer/Downloads/GDP_NK.xlsx", sheet = "Sheet1")
## New names:
## • `Year` -> `Year...1`
## • `` -> `...3`
## • `` -> `...4`
## • `Year` -> `Year...5`
# Ensure the 'Year' column is treated as an integer
gdp_data_cleaned <- gdp_data[, c("Year...1", "GDP Growth  Rates")]
colnames(gdp_data_cleaned) <- c("Year", "GDP_Growth_Rates")
gdp_data_cleaned$Year <- as.integer(gdp_data_cleaned$Year)
imports_data$Year <- as.integer(imports_data$Year)

# Merge the cleaned GDP data with the imports data based on the Year column
merged_data <- merge(imports_data, gdp_data_cleaned, by = "Year")
# Create the plot using ggplot2
p <- ggplot(merged_data, aes(x = Year)) +
  geom_line(aes(y = `Import (USD Thousands)`, color = "Imports from China"), size = 1.2) +
  geom_line(aes(y = GDP_Growth_Rates * 1000000, color = "GDP Growth"), size = 1.2) +  # Adjusting the scale for visualization
  scale_y_continuous(
    name = "Imports (USD Thousands)",
    sec.axis = sec_axis(~./1000000, name = "GDP Growth (%)")  # Adjusting the secondary axis
  ) +
  scale_x_continuous(breaks = seq(min(merged_data$Year), max(merged_data$Year), by = 1)) +  # Specify the breaks
  labs(
    title = "North Korea's Imports from China and GDP Growth (2013-2022)",
    x = "Year"
  ) +
  theme_minimal() +
  scale_color_manual(
    name = "Legend",
    values = c("Imports from China" = "blue", "GDP Growth" = "red")
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Convert the ggplot to an interactive plotly object
interactive_plot <- ggplotly(p)

# Display the interactive plot
interactive_plot